import pygame
from typing import Tuple, Optional

class ScreenEffects:
    def __init__(self):
        self.shake_time = 0
        self.shake_intensity = 0
        self.flash_time = 0
        self.flash_color = None
        self.glow_time = 0
        self.glow_color = None
    
    def add_shake(self, duration: float, intensity: float):
        """添加震动效果"""
        self.shake_time = duration
        self.shake_intensity = intensity
    
    def add_flash(self, duration: float, color: Tuple[int, int, int]):
        """添加闪光效果"""
        self.flash_time = duration
        self.flash_color = color
    
    def add_glow(self, duration: float, color: Tuple[int, int, int]):
        """添加发光效果"""
        self.glow_time = duration
        self.glow_color = color
    
    def update(self, dt: float) -> Tuple[int, int]:
        """更新特效并返回屏幕偏移量"""
        # 更新震动
        self.shake_time = max(0, self.shake_time - dt)
        offset_x = 0
        offset_y = 0
        if self.shake_time > 0:
            import random
            offset_x = random.randint(-int(self.shake_intensity), int(self.shake_intensity))
            offset_y = random.randint(-int(self.shake_intensity), int(self.shake_intensity))
        
        # 更新闪光
        self.flash_time = max(0, self.flash_time - dt)
        
        # 更新发光
        self.glow_time = max(0, self.glow_time - dt)
        
        return (offset_x, offset_y)
    
    def draw(self, surface: pygame.Surface):
        """绘制特效"""
        # 绘制闪光
        if self.flash_time > 0:
            flash_surface = pygame.Surface(surface.get_size(), pygame.SRCALPHA)
            alpha = int(255 * (self.flash_time / 0.2))  # 0.2秒内渐变
            flash_surface.fill((*self.flash_color, min(alpha, 128)))
            surface.blit(flash_surface, (0, 0))
        
        # 绘制发光
        if self.glow_time > 0:
            glow_surface = pygame.Surface(surface.get_size(), pygame.SRCALPHA)
            alpha = int(255 * (self.glow_time / 0.3))  # 0.3秒内渐变
            glow_surface.fill((*self.glow_color, min(alpha, 100)))
            surface.blit(glow_surface, (0, 0)) 